package com.stef.saas.common.base.dto;

import com.stef.saas.common.base.constant.CommonConstants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author stef
 */
@Data
@ApiModel(value="基础返回实体")
public class BaseResult<T>  implements Serializable {

	/**
	 * 返回码：0为成功,非0则失败
	 */
	@ApiModelProperty(name = "getCode", dataType = "int", value = "响应码")
	private int code;
	/**
	 * 响应提示信息
	 */
	@ApiModelProperty(name = "message", dataType = "string", value = "响应信息")
	private String message;
	/**
	 * 返回数据
	 */
	@ApiModelProperty(name = "data", dataType = "object", value = "数据内容")
	private T data;

	public static <T> BaseResult<T> ok() {
		return restResult(null, CommonConstants.SUCCESS, null);
	}

	public static <T> BaseResult<T> ok(T data) {
		return restResult(data, CommonConstants.SUCCESS, null);
	}

	public static <T> BaseResult<T> ok(T data, String msg) {
		return restResult(data, CommonConstants.SUCCESS, msg);
	}

	public static <T> BaseResult<T> error() {
		return restResult(null, CommonConstants.FAIL, null);
	}

	public static <T> BaseResult<T> error(String msg) {
		return restResult(null, CommonConstants.FAIL, msg);
	}

	public static <T> BaseResult<T> error(int code,String msg) {
		return restResult(null, code, msg);
	}

	public static <T> BaseResult<T> error(T data) {
		return restResult(data, CommonConstants.FAIL, null);
	}

	public static <T> BaseResult<T> error(T data, String msg) {
		return restResult(data, CommonConstants.FAIL, msg);
	}

	private static <T> BaseResult<T> restResult(T data, int code, String msg) {
		BaseResult<T> apiResult = new BaseResult<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMessage(msg);
		return apiResult;
	}

}
